'''
====================
limber.ui.mainwindow
====================

LimberEditor's main window.
'''

from PyQt4 import QtCore, QtGui

from limber.ui.tools import create_action, add_actions
from limber.viewdisplayers import tabbedviewdisplayer
from limber import utils


class MainWindow(QtGui.QMainWindow):
    '''LimberEditor's mainwindow class.'''
    
    create_new_document_request = QtCore.pyqtSignal(name='createNewDocumentRequest')
    close_current_document_request = QtCore.pyqtSignal(name='closeCurrentDocumentRequest')
    
    application_closure_request = QtCore.pyqtSignal(name='applicationClosureRequest')
    
    def __init__(self, parent=None, viewdisplayer=None):
        super(MainWindow, self).__init__(parent)

        if viewdisplayer is None:
            viewdisplayer = tabbedviewdisplayer.TabbedViewDisplayer

        self.viewdisplayer = viewdisplayer(self)
        self.setCentralWidget(self.viewdisplayer)

        self.init_actions()
        self.init_menus()

        self.resume_settings()

    def init_actions(self):
        '''Initializes the actions of LimberEditor.'''
        
        file_new_document_action_args = {
            'text' : '&New Document...', 
            'parent' : self, 
            'slot' : self.create_new_document_request.emit, 
            'shortcut' : 'Ctrl+N', 
            'tool_tip' : 'Creates a new document.', 
            'propagate_tips' : True, 
            }
        
        file_new_document_action = create_action(**file_new_document_action_args)
        
        file_close_document_action_args = {
            'parent' : self, 
            'text' : 'Close Document', 
            'slot' : self.close_current_document_request.emit, 
            'shortcut' : 'Ctrl+Alt+Q', 
            'tool_tip' : 'Closes the current document.', 
            'propagate_tips' : True, 
            }
        
        file_close_document_action = create_action(**file_close_document_action_args)
        
        file_quit_action_args = {
            'parent' : self, 
            'text' : '&Quit', 
            'slot' : self.application_closure_request.emit, 
            'shortcut' : 'Ctrl+Q', 
            'tool_tip' : 'Quits LimberEditor', 
            'propagate_tips' : True, 
            }
        
        file_quit_action = create_action(**file_quit_action_args)

        self.file_menu_actions = [
            file_new_document_action,
            None,
            file_close_document_action,
            None,
            file_quit_action,
            ]

    def init_menus(self):
        '''Initializes the menus of LimberEditor.'''

        self.file_menu = self.menuBar().addMenu('&File...')
        add_actions(self.file_menu, self.file_menu_actions)

    def resume_settings(self):
        '''Resumes the last settings.'''

        pass
     
    def closeEvent(self, event):
        '''Closes the mainwindow.'''
        
        try:
            if event.must_close:
                super(MainWindow, self).closeEvent(event)
                return
        except AttributeError:
            pass
        
        event.ignore()
        self.application_closure_request.emit()
